﻿using Sirenix.OdinInspector;
using UnityEngine;

namespace HotPlayer.Physic2D
{
    public class TestSAT : MonoBehaviour
    {
        public HotPolygonCollider2D collider1;
        
        public HotPolygonCollider2D collider2;

        [Button]
        public void TestCollide()
        {
            collider1.RefreshShape();
            collider2.RefreshShape();
            
            var isCollide = HotPhysic2DEngine.TestCollideSAT(collider1.shape, collider2.shape, out var direction, out var mtv, out var contactPoint);
            Debug.Log($"{isCollide}, {direction}, {direction.magnitude}, mtv: {mtv}, {contactPoint}");
            if (isCollide)
            {
                var endPoint = contactPoint + direction * mtv;
                var contactPoint3D = new Vector3(contactPoint.x, 0, contactPoint.y);
                var shape2Center = collider2.shape.AveragePoint();
                var direction3D = new Vector3(direction.x, 0, direction.y)* 10;
                var shape2Center3D = new Vector3(shape2Center.x, 0, shape2Center.y);
                Debug.DrawLine(shape2Center3D - direction3D, shape2Center3D + direction3D, Color.blue, 10, false);
                Debug.DrawLine(contactPoint3D, contactPoint3D + new Vector3(0,10,0), Color.yellow, 10, false);
                Debug.DrawLine(contactPoint3D, new Vector3(endPoint.x, 0, endPoint.y), Color.red, 10, false);
            }
        }
    }
}